package com.rxyb.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxyb.authority.constants.AuthorityConstants;
import com.rxyb.authority.db.dao.OpsAuthorityRoleMenuMapper;
import com.rxyb.authority.db.po.OpsAuthorityRoleMenu;
import com.rxyb.authority.service.OpsAuthorityRoleMenuService;
import com.rxyb.toolize.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DUYH
 * @since 2020-05-29
 */
@Service
public class OpsAuthorityRoleMenuServiceImpl extends ServiceImpl<OpsAuthorityRoleMenuMapper, OpsAuthorityRoleMenu> implements OpsAuthorityRoleMenuService {

    @Autowired(required = false)
    private OpsAuthorityRoleMenuMapper authorityRoleMenuMapper;

    @Override
    public boolean create(String roleId, List<String> menuIds) {
        long addTime = DateTime.getDateTime();
        int size = 0;
        for (String menuId : menuIds) {
            OpsAuthorityRoleMenu record = new OpsAuthorityRoleMenu();
            record.setRowState(AuthorityConstants.ROWSTATE_NORMAL);
            record.setMenuId(menuId);
            record.setRoleId(roleId);
            record.setAddTime(addTime);
            record.setUpdateTime(addTime);
            int cnt = authorityRoleMenuMapper.insert(record);
            if (cnt == 1) {
                size = size + 1;
            } else {
                log.error("插入角色关联菜单之一失败：roleId=[{" + roleId + "}],menuId=[{" + menuId + "}]");
            }
        }
        if (menuIds.size() == size) {
            log.debug("插入角色关联菜单全部成功");
            return true;
        }
        log.error("插入角色关联菜单结果，预计插入[{" + menuIds + "}]，实际插入[{" + size + "}]");
        return false;
    }

    @Override
    public boolean update(String roleId, List<String> menuIds) {
        // 删除角色关联菜单
        boolean deleteF = remove(roleId);
        if (!deleteF) {
            log.debug("该角色没有已授权菜单");
        }
        // 创建角色关联菜单
        boolean insertF = create(roleId, menuIds);
        if (!insertF) {
            return false;
        }
        return true;
    }

    @Override
    public boolean remove(String roleId) {
        // 需要修改的参数
        OpsAuthorityRoleMenu record = new OpsAuthorityRoleMenu();
        record.setRowState(AuthorityConstants.ROWSTATE_DELETE);
        record.setUpdateTime(DateTime.getDateTime());
        QueryWrapper<OpsAuthorityRoleMenu> wrapper = new QueryWrapper<OpsAuthorityRoleMenu>();
        wrapper.lambda().eq(OpsAuthorityRoleMenu::getId, roleId);
        wrapper.lambda().eq(OpsAuthorityRoleMenu::getRowState, AuthorityConstants.ROWSTATE_NORMAL);
        int cnt = authorityRoleMenuMapper.update(record, wrapper);
        // 可能存在菜单没关联角色的情况，默认成功
        return true;
    }

    @Override
    @Cacheable(cacheNames = {"rolemenu"}, key = "#roleIds", unless = "#result == null")
    public List<OpsAuthorityRoleMenu> queryMenusByRole(List<String> roleIds) {
        QueryWrapper<OpsAuthorityRoleMenu> wrapper = new QueryWrapper<OpsAuthorityRoleMenu>();
        wrapper.lambda().in(OpsAuthorityRoleMenu::getRoleId, roleIds);
        wrapper.lambda().eq(OpsAuthorityRoleMenu::getRowState, AuthorityConstants.ROWSTATE_NORMAL);
        return authorityRoleMenuMapper.selectList(wrapper);
    }

    @Override
    @Cacheable(cacheNames = {"rolemenu"}, key = "#menuIds", unless = "#result == null")
    public List<OpsAuthorityRoleMenu> queryRolesByMenu(List<String> menuIds) {
        QueryWrapper<OpsAuthorityRoleMenu> wrapper = new QueryWrapper<OpsAuthorityRoleMenu>();
        wrapper.lambda().in(OpsAuthorityRoleMenu::getMenuId, menuIds);
        wrapper.lambda().eq(OpsAuthorityRoleMenu::getRowState, AuthorityConstants.ROWSTATE_NORMAL);
        return authorityRoleMenuMapper.selectList(wrapper);
    }
}
